package com.sky;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.MediaType;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

/**
 * @version 1.0
 * @description 说明
 * @package com.sky
 */
public class HttpClientDemo {
    public static void main(String[] args) throws Exception {
        //httpGet();
        //System.out.println(MediaType.APPLICATION_JSON.toString());
        //httpPost();
        mpLogin();
    }

    private static void mpLogin() throws Exception {
        //1. 创建client对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2. 创建请求对象
        String code = "0f3zQAll21PGLd4wI9ml2bEH0n3zQAlA";
        String url = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=wxae41e94af6d4e052&secret=8d123a963913882dd2244c0b0335c66e&" +
            "js_code=%s" +
            "&grant_type=authorization_code",code);
        HttpGet httpGet = new HttpGet(url);

        //3. 发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4. 解析响应的结果
        // http响应的状态码
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("statusCode = " + statusCode);
        // 响应内容
        HttpEntity entity = response.getEntity();
        // 响应体转成json字符串 {"session_key":"nbY8Ie7ujPDMApBS+aljCA==","openid":"oLp547U7bPsQQAKbjCE1IwA4jALA"}
        String responseBodyJson = EntityUtils.toString(entity);
        // 解析json字符串为java bean
        JSONObject jsonObject = JSON.parseObject(responseBodyJson);
        // 读取json对象中指定key的值,key=openid, 微信返回的openid=oLp547U7bPsQQAKbjCE1IwA4jALA
        String openid = jsonObject.getString("openid");
        System.out.println("openid = " + openid);
        //5. 关闭资源
        response.close();
        httpClient.close();
    }

    private static void httpPost() throws IOException {
        //1. 创建client对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2. 创建请求对象
        HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");

        // 构建请求体对象
        // 设置请求参数是一个json字符串
        StringEntity stringEntity = buildData();
        httpPost.setEntity(stringEntity);
        //3. 发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        //4. 解析响应的结果
        // http响应的状态码
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("statusCode = " + statusCode);
        // 响应内容
        HttpEntity entity = response.getEntity();
        System.out.println("EntityUtils.toString(entity) = " + EntityUtils.toString(entity));
        //5. 关闭资源
        response.close();
        httpClient.close();
    }

    private static StringEntity buildData() throws UnsupportedEncodingException {
        /*String json = "{\n" +
        "  \"password\": \"123456\",\n" +
        "  \"username\": \"admin\"\n" +
        "}";*/
        JSONObject jsonData = new JSONObject();
        jsonData.put("password","123456");
        jsonData.put("username","admin");
        String jsonDataString = jsonData.toJSONString();

        StringEntity stringEntity = new StringEntity(jsonDataString);
        // 请求体的编码为utf-8
        stringEntity.setContentEncoding("utf-8");
        // 请求的格式
        stringEntity.setContentType(MediaType.APPLICATION_JSON.toString());

        return stringEntity;
    }

    private static void httpGet() throws IOException {
        //1. 创建client对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2. 创建请求对象
        HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
        //3. 发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4. 解析响应的结果
        // http响应的状态码
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("statusCode = " + statusCode);
        // 响应内容
        HttpEntity entity = response.getEntity();
        System.out.println("EntityUtils.toString(entity) = " + EntityUtils.toString(entity));
        //5. 关闭资源
        response.close();
        httpClient.close();
    }
}
